package com.rss.provider.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.rss.provider.constant.NavigationConstant;
import com.rss.provider.dao.RssDao;
import com.rss.provider.hibernate.entity.SubscriberEntity;
import com.rss.provider.hibernate.entity.UpdateEntity;

@Repository("RssDaoImpl")
public class RssDaoImpl extends HibernateDaoSupport implements RssDao {

	/**
	 * Initiate Logger for this class
	 */
	private static final Log logger = LogFactory
			.getLog(RssDaoImpl.class);
	
	
	@Autowired
	@Qualifier("psessionFactory")
	public void injectSessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public String addSubscriber(SubscriberEntity subscriberEntity) {
		List<SubscriberEntity> entities = getHibernateTemplate().find("from SubscriberEntity as se where se.email=? and se.subscribedTo=?", subscriberEntity.getEmail(), subscriberEntity.getSubscribedTo());
		if(entities!=null && entities.size()>0){
			if(logger.isWarnEnabled()){
				logger.warn("user already exists.");
			}
			return NavigationConstant.FAILED;
		}
		getHibernateTemplate().save(subscriberEntity);
		return NavigationConstant.SUCCESS;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String removeSubscriber(SubscriberEntity unsubscriberEntity) {
		List<SubscriberEntity> entities = getHibernateTemplate().find("from SubscriberEntity as se where se.name=? and se.email=? and se.subscribedTo=?", unsubscriberEntity.getName(), unsubscriberEntity.getEmail(), unsubscriberEntity.getSubscribedTo());
		if(entities!=null && entities.size()>0){
			getHibernateTemplate().delete(entities.get(0));
			return NavigationConstant.SUCCESS;
		}
		if(logger.isWarnEnabled()){
			logger.warn("user does not exist.");
		}
		return NavigationConstant.FAILED;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SubscriberEntity> getAllSubscribers(String subscribedTo) {
		List<SubscriberEntity> subscriberEntities = getHibernateTemplate().find("from SubscriberEntity as se where se.subscribedTo=?", subscribedTo);
		if(subscriberEntities!=null && subscriberEntities.size()>0)
			return subscriberEntities;
		return null;
	}

	@Override
	public String saveUpdate(UpdateEntity updateEntity) {
		getHibernateTemplate().save(updateEntity);
		return NavigationConstant.SUCCESS;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UpdateEntity> getUpdates(Date fromDate) {
		List<UpdateEntity> updateEntities = getHibernateTemplate().find("from UpdateEntity as ue where ue.cdate=?", fromDate);
		if(updateEntities!=null && updateEntities.size()>0)
			return updateEntities;
		return null;
	}
}
